#include "AntiGrav.h"
#include <math.h>

// Diffuse lighting for antigrav
//GLfloat antigrav_diffuse[] = {0.50, 0.50, 0.50, 1.0};

GLfloat antigrav_diffuse[] = {0.7, 0.7, 0.7, 1.0};
GLfloat antigrav_ambient[] = {0.7, 0.7, 0.7, 1.0};
GLfloat antigrav_specular[] = {0.5, 0.5, 0.5, 1.0};

// Shininess
GLfloat antigrav_shininess = 10.0f;

AntiGrav::AntiGrav(void)
{
	sizeBase = 1.0;
	//heightBase = 0.3;
	heightBase = 0.5;
	sizeLeg = 0.05;
	//lenghtLeg = 0.7;
	lenghtLeg = 0.3;
	sizeCylinder = 0.8;
	quad = gluNewQuadric();
	loadImage();
}

void AntiGrav::loadImage()
{
	Image* image1 = loadBMP("textures\\textureAntigrav.bmp");
	Image* image2 = loadBMP("textures\\textureAntigrav2.bmp");
	Image* image3 = loadBMP("textures\\alu.bmp");
	Image* image4 = loadBMP("textures\\copper.bmp");
	textureBase1 = loadTexture(image1);
	textureBase2 = loadTexture(image2);
	textureLeg1 = loadTexture(image3);
	textureLeg2 = loadTexture(image4);
	
	currentTextureBase = textureBase1;
	currentTextureLeg = textureLeg1;

	delete image1;
	delete image2;
	delete image3;
	delete image4;
}

void AntiGrav::switchTexture()
{
	if(currentTextureBase == textureBase1)
		currentTextureBase = textureBase2;
	else
		currentTextureBase = textureBase1;
	if(currentTextureLeg == textureLeg1)
		currentTextureLeg = textureLeg2;
	else
		currentTextureLeg = textureLeg1;
}

GLuint AntiGrav::getTexture()
{
	return currentTextureBase;
}

void AntiGrav::display()
{

	glPushMatrix();
		glTranslatef(0.0, elevation, 0.0);

		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, antigrav_diffuse);
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, antigrav_ambient );
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, antigrav_specular);
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, antigrav_shininess);

		glColor3f(1,1,1);
		glTranslatef(0.0, heightBase/2 + lenghtLeg, 0.0);

		// CUBE
		glPushMatrix();			
			glBindTexture(GL_TEXTURE_2D, currentTextureBase);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			
			glScalef(sizeBase, heightBase, sizeBase);
			drawCube(0.5, 0.75);
		glPopMatrix();

		// LEGS
		glPushMatrix();
			gluQuadricNormals(quad, GLU_SMOOTH);   // Create Smooth Normals (better for light)
			gluQuadricTexture(quad, GL_TRUE);      // Create Texture Coords
			
			glBindTexture(GL_TEXTURE_2D, currentTextureLeg);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
			glColor3f(1.0f, 1.0f, 1.0f);
			
			glPushMatrix();
				glTranslatef(sizeBase/3, -heightBase/2, sizeBase/3);
				glRotatef(90, 1.0, 0.0, 0.0);
				gluCylinder (quad, sizeLeg, sizeLeg*1.5, lenghtLeg, 10, 30);
			glPopMatrix();

			glPushMatrix();
				glTranslatef(-sizeBase/3, -heightBase/2, sizeBase/3);
				glRotatef(90, 1.0, 0.0, 0.0);
				gluCylinder (quad, sizeLeg, sizeLeg*1.5, lenghtLeg, 10, 30);
			glPopMatrix();

			glPushMatrix();
				glTranslatef(-sizeBase/3, -heightBase/2, -sizeBase/3);
				glRotatef(90, 1.0, 0.0, 0.0);
				gluCylinder (quad, sizeLeg, sizeLeg*1.5, lenghtLeg, 10, 30);
			glPopMatrix();

			glPushMatrix();
				glTranslatef(sizeBase/3, -heightBase/2, -sizeBase/3);
				glRotatef(90, 1.0, 0.0, 0.0);
				gluCylinder (quad, sizeLeg, sizeLeg*1.5, lenghtLeg, 10, 30);
			glPopMatrix();
		glPopMatrix();
	glPopMatrix();
}

float AntiGrav::getHeight()
{
	return heightBase + lenghtLeg;
}

AntiGrav::~AntiGrav(void)
{
	gluDeleteQuadric(quad);
}
